package com.gao.config;

import com.gao.interceptors.FirstInterceptor;
import com.sun.deploy.util.Property;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import java.util.List;
import java.util.Properties;

/**
 * 相当于SpringMVC配置xml
 *
 * 1.mvc静态资源访问 √ 2.mvc驱动注解 √
 * 3.视图解析器配置 √  4.拦截器配置 √
 * 5.视图控制器配置 √  6.异常处理配置 √
 * 7.文件上传解析器 √   8.包扫描 √
 *
 *
 *
 *
 *
 * @author GaoXi
 * @date 2021/8/17 20:45
 */
@Configuration
//8.包扫描
@ComponentScan("com.gao")
//2.mvc驱动注解
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    /**
     * 1.mvc静态资源访问
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
       configurer.enable();
    }

    /**
     * 5.视图控制器
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/hello").setViewName("hello");
    }
    // ========================视图解析器=start=========================
    /**
     *  <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
     *
     *                     <!-- 视图前缀 -->
     *                     <property name="prefix" value="/WEB-INF/templates/"/>
     *
     *                     <!-- 视图后缀 -->
     *                     <property name="suffix" value=".html"/>
     *                     <property name="templateMode" value="HTML5"/>
     *                     <property name="characterEncoding" value="UTF-8" />
     *                 </bean>
     */
    @Bean
    public SpringResourceTemplateResolver springResourceTemplateResolver(){
        SpringResourceTemplateResolver springResourceTemplateResolver = new SpringResourceTemplateResolver();
        springResourceTemplateResolver.setSuffix(".html");
        springResourceTemplateResolver.setPrefix("/WEB-INF/templates/");
        springResourceTemplateResolver.setTemplateMode("HTML5");
        springResourceTemplateResolver.setCharacterEncoding("UTF-8");
        return springResourceTemplateResolver;
    }

    /**
     *  <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
     *             <property name="templateResolver">
     *                 <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
     *                      ....
     *                 </bean>
     *             </property>
     *         </bean>
     */
    @Bean
    public SpringTemplateEngine springTemplateEngine(SpringResourceTemplateResolver springResourceTemplateResolver){
        SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
        springTemplateEngine.setTemplateResolver(springResourceTemplateResolver);
        return springTemplateEngine;
    }

    /**
     * <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
     *     <property name="order" value="1"/>
     *     <property name="characterEncoding" value="UTF-8"/>
     *     <property name="templateEngine">
     *         <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
     *             ....
     *         </bean>
     *     </property>
     * </bean>
     */
    @Bean
    public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine springTemplateEngine){
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
        thymeleafViewResolver.setOrder(1);
        thymeleafViewResolver.setCharacterEncoding("UTF-8");
        thymeleafViewResolver.setTemplateEngine(springTemplateEngine);
        return thymeleafViewResolver;
    }
    // ========================视图解析器=end===========================


    /**
     * 7.文件上传解析器
     */
    @Bean
    public CommonsMultipartResolver multipartResolver(){
        return new CommonsMultipartResolver();
    }


    /**
     * 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        FirstInterceptor firstInterceptor = new FirstInterceptor();
        registry.addInterceptor(firstInterceptor).addPathPatterns("/**")
            .excludePathPatterns("/testInterceptor","/testEx","/testFilter");
    }

    /**
     * 异常处理器
     */
    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();

        Properties properties = new Properties();
        properties.setProperty("java.lang.ArithmeticException","error");
        simpleMappingExceptionResolver.setExceptionMappings(properties);
        simpleMappingExceptionResolver.setExceptionAttribute("ex");
        resolvers.add(simpleMappingExceptionResolver);
    }
}
